package ufrj.safcp.demo.imagecapturing;

import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;

import ufrj.safcp.tools.imagecapturing.IImageCapturing;
import ufrj.safcp.tools.imagecapturing.IImageReceiver;
import ufrj.safcp.tools.imagecapturing.LTICivilImageCapturing;
import ufrj.safcp.view.components.ImagePanel;

import com.lti.civil.CaptureException;

public class ImageCaptureDemo extends JFrame implements IImageReceiver {

	private static final long serialVersionUID = 1L;

	private ImagePanel jPanel = null;
	
	private IImageCapturing captring = LTICivilImageCapturing.getInstance();

	public ImageCaptureDemo() {

		initialize();
		setVisible(true);
		
		captring.init(0, new Dimension(320, 240));
		captring.addImageReceiver(this);
		captring.start();

	}

	/**
	 * This method initializes this
	 * 
	 */
	private void initialize() {
		this.setSize(new Dimension(600, 400));
		this.setContentPane(getJPanel());
		this.setTitle("Demonstração captura de imagem.");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				captring.stop();
				captring.destroy();
				System.exit(0);
			}
		});
	}

	/**
	 * This method initializes jPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private ImagePanel getJPanel() {
		if (jPanel == null) {
			jPanel = new ImagePanel();
			jPanel.setLayout(new GridBagLayout());
			jPanel.setPreferredSize(new Dimension(352, 288));
		}
		return jPanel;
	}

	public static void main(String[] args) throws CaptureException {
		new ImageCaptureDemo();
	}

	public void receiveImage(Image bimg) {
		getJPanel().setImage(bimg);
	}
} // @jve:decl-index=0:visual-constraint="0,0"
